package controller

import (
	"admin/contrib/session"
	"admin/model"
	"common/helper"
	"encoding/json"
	"github.com/valyala/fasthttp"
	"strconv"
	"strings"
)

type AgentController struct{}

// 代理明细
func (this *AgentController) DetailList(ctx *fasthttp.RequestCtx) {
	param := model.ReportProxyParam{}
	err := json.Unmarshal(ctx.PostBody(), &param)
	if err != nil {

		helper.RetData(ctx, false, "参数错误")
		return
	}

	if param.Page < 1 {
		param.Page = 1
	}

	if param.PageSize < 10 || param.PageSize > 3000 {
		helper.RetData(ctx, false, "参数错误")
		return
	}

	s, err := model.ProxyDetailList(param, ctx)
	if err != nil {
		helper.RetData(ctx, false, err.Error())
		return
	}
	helper.RetData(ctx, true, s)
}

// 代理日报
func (this *AgentController) DailyList(ctx *fasthttp.RequestCtx) {
	param := model.ReportProxyParam{}
	err := json.Unmarshal(ctx.PostBody(), &param)
	if err != nil {

		helper.RetData(ctx, false, "参数错误")
		return
	}

	if param.Page < 1 {
		param.Page = 1
	}

	if param.PageSize < 10 || param.PageSize > 3000 {
		helper.RetData(ctx, false, "参数错误")
		return
	}

	s, err := model.ProxyDailyList(param, ctx)
	if err != nil {
		helper.RetData(ctx, false, err.Error())
		return
	}
	helper.RetData(ctx, true, s)
}

// 代理总报
func (this *AgentController) StatisticsList(ctx *fasthttp.RequestCtx) {
	param := model.ReportProxyParam{}
	err := json.Unmarshal(ctx.PostBody(), &param)
	if err != nil {

		helper.RetData(ctx, false, "参数错误")
		return
	}

	if param.Page < 1 {
		param.Page = 1
	}

	if param.PageSize < 10 || param.PageSize > 3000 {
		helper.RetData(ctx, false, "参数错误")
		return
	}

	s, err := model.ProxyStatisticsList(param, ctx)
	if err != nil {
		helper.RetData(ctx, false, err.Error())
		return
	}
	helper.RetData(ctx, true, s)
}

// 代理汇总
func (this *AgentController) SummaryList(ctx *fasthttp.RequestCtx) {
	param := model.ReportProxyParam{}
	err := json.Unmarshal(ctx.PostBody(), &param)
	if err != nil {

		helper.RetData(ctx, false, "参数错误")
		return
	}

	if param.Page < 1 {
		param.Page = 1
	}

	if param.PageSize < 10 || param.PageSize > 3000 {
		helper.RetData(ctx, false, "参数错误")
		return
	}

	s, err := model.ProxySummaryList(param, ctx)
	if err != nil {
		helper.RetData(ctx, false, err.Error())
		return
	}
	helper.RetData(ctx, true, s)
}

// 代理关系
func (this *AgentController) RealtionList(ctx *fasthttp.RequestCtx) {
	param := model.ReportProxyRelationParam{}
	err := json.Unmarshal(ctx.PostBody(), &param)
	if err != nil {

		helper.RetData(ctx, false, "参数错误")
		return
	}

	if param.Page < 1 {
		param.Page = 1
	}

	if param.PageSize < 10 || param.PageSize > 3000 {
		helper.RetData(ctx, false, "参数错误")
		return
	}

	s, err := model.ProxyRealtionList(param, ctx)
	if err != nil {
		helper.RetData(ctx, false, err.Error())
		return
	}
	helper.RetData(ctx, true, s)
}

// 代理周返佣
func (this *AgentController) WeekCommiList(ctx *fasthttp.RequestCtx) {

	param := model.CommonParam{}
	err := json.Unmarshal(ctx.PostBody(), &param)
	if err != nil {

		helper.RetData(ctx, false, "参数错误")
		return
	}

	if param.Page < 1 {
		param.Page = 1
	}

	if param.PageSize < 10 || param.PageSize > 3000 {
		helper.RetData(ctx, false, "参数错误")
		return
	}

	s, err := model.WeekCommiList(param, ctx)
	if err != nil {
		helper.RetData(ctx, false, err.Error())
		return
	}
	helper.RetData(ctx, true, s)
}

// 博主数据监控
func (this *AgentController) BolgerList(ctx *fasthttp.RequestCtx) {
	param := model.ReportProxyParam{}
	err := json.Unmarshal(ctx.PostBody(), &param)
	if err != nil {

		helper.RetData(ctx, false, "参数错误")
		return
	}

	if param.Page < 1 {
		param.Page = 1
	}

	if param.PageSize < 10 || param.PageSize > 3000 {
		helper.RetData(ctx, false, "参数错误")
		return
	}

	//s, err := model.GetBolgerList(param, ctx)
	s, err := model.GetBolgerList2(param, ctx)
	if err != nil {
		helper.RetData(ctx, false, err.Error())
		return
	}
	helper.RetData(ctx, true, s)
}

func (this *AgentController) BolgerInsert(ctx *fasthttp.RequestCtx) {
	param := model.BolgerData{}
	err := json.Unmarshal(ctx.PostBody(), &param)
	if err != nil {

		helper.RetData(ctx, false, "参数错误")
		return
	}
	userInfo, err := model.MemberFindByUid(param.Uid)
	auth, err := model.UserAuthCheck(userInfo, ctx)
	if auth == false || err != nil {
		helper.RetData(ctx, false, "非本渠道玩家")
		return
	}

	err = model.BolgerUpdate(param, 0)
	if err != nil {
		helper.RetData(ctx, false, err.Error())
		return
	}
	helper.RetData(ctx, true, "成功")
}

func (this *AgentController) BolgerDelete(ctx *fasthttp.RequestCtx) {
	BolgerDeleteData := model.BolgerDeleteData{}
	err := json.Unmarshal(ctx.PostBody(), &BolgerDeleteData)
	if err != nil {

		helper.RetData(ctx, false, "参数错误")
		return
	}
	param := model.BolgerData{}
	param.Uid = BolgerDeleteData.Id
	userInfo, err := model.MemberFindByUid(param.Uid)
	auth, err := model.UserAuthCheck(userInfo, ctx)
	if auth == false || err != nil {
		helper.RetData(ctx, false, err.Error())
		return
	}

	err = model.BolgerUpdate(param, 1)
	if err != nil {
		helper.RetData(ctx, false, err.Error())
		return
	}
	helper.RetData(ctx, true, "成功")
}

// 掉绑明细
func (this *AgentController) UnbindList(ctx *fasthttp.RequestCtx) {
	param := model.ReportProxyParam{}
	err := json.Unmarshal(ctx.PostBody(), &param)
	if err != nil {

		helper.RetData(ctx, false, "参数错误")
		return
	}

	if param.Page < 1 {
		param.Page = 1
	}

	if param.PageSize < 10 || param.PageSize > 3000 {
		helper.RetData(ctx, false, "参数错误")
		return
	}

	s, err := model.GetUnbindList(param, ctx)
	if err != nil {
		helper.RetData(ctx, false, err.Error())
		return
	}
	helper.RetData(ctx, true, s)
}

// 解绑 绑定 上级
func (this *AgentController) ChangeParent(ctx *fasthttp.RequestCtx) {
	param := model.ChangeParentParam{}

	//model.InsertLog("解绑换绑上级", ctx)

	err := json.Unmarshal(ctx.PostBody(), &param)
	if err != nil {

		helper.RetData(ctx, false, "参数错误")
		return
	}
	userInfo, err := model.MemberFindByUid(strconv.Itoa(param.Uid))
	auth, err := model.UserAuthCheck(userInfo, ctx)
	if auth == false || err != nil {
		helper.RetData(ctx, false, err.Error())
		return
	}

	parentInfo, _ := model.MemberFindByUid(strconv.Itoa(param.ParentId))
	if param.ParentId > 0 {
		if parentInfo.Uid == "" {
			helper.RetData(ctx, false, "参数错误")
			return
		}
	} else {
		parentInfo.OperatorId = "0"
		parentInfo.BusinessId = "0"

		parentInfo.OperatorId = userInfo.OperatorId
		parentInfo.BusinessId = userInfo.BusinessId
	}
	if param.Uid == param.ParentId {
		helper.RetData(ctx, false, "不能挂靠自身")
		return
	}
	//获取全部下级
	uids := model.GetAgentTeam(strconv.Itoa(param.Uid), strconv.Itoa(param.Uid))
	arrUids := strings.Split(uids, ",")
	for _, value := range arrUids {
		if value == strconv.Itoa(param.ParentId) {
			helper.RetData(ctx, false, "不能挂靠下级")
			return
		}
	}

	beforParentId := model.GetParent(param.Uid)

	param.BeforeParentId = beforParentId
	param.BeforeBusinessId = userInfo.BusinessId
	param.BeforeOperatorId = userInfo.OperatorId

	param.BusinessId = parentInfo.BusinessId
	param.OperatorId = parentInfo.OperatorId

	admin_info := session.AdminGet(ctx)
	opt_id, _ := strconv.Atoi(admin_info["id"])

	record, _ := json.Marshal(param)
	//ip := helper.FromRequest(ctx)
	model.InsertOptLog(1, opt_id, "解绑/换绑", string(ctx.Path()), string(record), "127.0.0.1")

	err = model.ChanggeParent(param.Uid, param.ParentId, parentInfo.OperatorId, parentInfo.BusinessId, ctx)
	if err != nil {
		helper.RetData(ctx, false, err.Error())
		return
	}
	helper.RetData(ctx, true, "成功")
}

func (this *AgentController) BatchUpdateState(ctx *fasthttp.RequestCtx) {
	param := model.BatchUpdateStateParam{}

	model.InsertLog("代理批量封禁", ctx)

	err := json.Unmarshal(ctx.PostBody(), &param)
	if err != nil {

		helper.RetData(ctx, false, "参数错误")
		return
	}
	err = model.BatchUpdateState(param, ctx)
	if err != nil {
		helper.RetData(ctx, false, err.Error())
		return
	}
	helper.RetData(ctx, true, "成功")
}
